package com.noah.note.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.noah.note.R;

import java.util.List;

public class HomePagerAdapter extends FragmentStatePagerAdapter {
    private Context mContext;
    private FragmentManager fm;
    private List<Fragment> fragments;
    private boolean[] fragmentsUpdateFlag;

    public HomePagerAdapter(Context context, FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fm = fm;
        this.fragments = fragments;
        this.mContext = context;
        fragmentsUpdateFlag = new boolean[fragments.size()];
        for (int i = 0; i < fragments.size(); i++) {
            fragmentsUpdateFlag[i] = false;
        }
    }


    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
    }


    @Override
    public Fragment getItem(int position) {
        Fragment fragment = fragments.get(position % fragments.size());
        return fragments.get(position % fragments.size());
    }


    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //得到缓存的fragment
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        //得到tag，这点很重要
        String fragmentTag = fragment.getTag();
        if (fragmentsUpdateFlag[position % fragmentsUpdateFlag.length]) {
            //如果这个fragment需要更新
            FragmentTransaction ft = fm.beginTransaction();
            //移除旧的fragment
            ft.remove(fragment);
            //换成新的fragment
            fragment = fragments.get(position % fragments.size());
            //添加新fragment时必须用前面获得的tag，这点很重要
            ft.add(container.getId(), fragment, fragmentTag);
            ft.attach(fragment);
            ft.commit();
            //复位更新标志
            fragmentsUpdateFlag[position % fragmentsUpdateFlag.length] = false;
        }
        return fragment;
    }

    /**
     * 使用我们自定义的布局时，这里返回null
     *
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return null;
    }

    /**
     * 定义一个方法 返回tab中要显示的内容；
     * 注意：在我们使用自定义的tab时，将getPagerTitle返回null，不设置也一样的
     *
     * @param position
     * @return
     */
    public View getCustomTabView(int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bottom_tab, null);
        if(0 == position){
            ImageView ivTabBottom = view.findViewById(R.id.ivTabBottom);
            ivTabBottom.setImageResource(R.drawable.ic_home_black);
        }
        return view;
    }
}